package com.xuan.yubi.bizmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.xuan.yubi.constant.RabbitMqConstant;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;


@Configuration
@ConfigurationProperties(prefix = "spring.rabbit")
@Data
@Slf4j
/*
  初始化(创建)交换机和消息队列，并绑定队列和交换机的路由键
 */
public class BiInitMain {

    private static String host;

    private static int port;

    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(host);
        try {
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            channel.exchangeDeclare(RabbitMqConstant.EXCHANGE_NAME, "direct", true);
            channel.queueDeclare(RabbitMqConstant.QUEUE_NAME, true, false, false, null);
            channel.queueBind(RabbitMqConstant.QUEUE_NAME, RabbitMqConstant.EXCHANGE_NAME, RabbitMqConstant.ROUTING_KEY, null);
        } catch (Exception e) {
            log.error("rabbitmq connect error", e);
        }

    }
}
